package com.bobi.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bobi.entity.Promotion;
import com.bobi.entity.PurchaseOrder;
import com.bobi.entity.ShoppingOrder;
import com.bobi.enums.ResultCode;
import com.bobi.service.SalesService;
import com.bobi.service.SalesService;
import com.bobi.utils.PageUtil;
import com.bobi.utils.ResultUtil;
import com.bobi.vo.PageVO;
import com.bobi.vo.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wenyuan
 * @version 1.0
 * @date 2023/7/4
 * @since 8
 */
@RestController
@RequestMapping("/sales")
public class SalesController {

    @Autowired
    private SalesService SalesService;

    /**
     * 分页查询所有信息
     * @param pageVO 分页对象
     * @return 返回分页对象
     */
    @GetMapping(value=  "/page")
    public ResultMessage<Page<ShoppingOrder>> page(PageVO pageVO){
        Page<ShoppingOrder> page = PageUtil.initPage(pageVO);
        Page<ShoppingOrder> list =SalesService.page(page);

        return ResultUtil.data(list);
    }


//    @PutMapping
//    public ResultMessage addPurchaseOrder(ShoppingOrder shoppingOrder
//        if(SalesService.save(ShoppingOrder)){
//            return ResultUtil.success();
//        }
//        return ResultUtil.error(400013,"失败");
//    }

    /**
     * 更新活动
     * @param shoppingOrder 活动对象
     * @return 返回对象
     */
    // 修改
    @PutMapping("/up/{id}")
    public ResultMessage updateMember(@PathVariable("id") String id, @RequestBody ShoppingOrder shoppingOrder) {
        // 构造查询条件包装
        UpdateWrapper<ShoppingOrder> updateWrapper = new UpdateWrapper<ShoppingOrder>().eq("id", id);
        // 如果更新成功
        if (SalesService.update(shoppingOrder, updateWrapper)) {
            return ResultUtil.success();
        }
        return ResultUtil.error(40008, "更新失败！");
    }


//    @PostMapping  (value = "/up/{id}")
//    public ResultMessage<PurchaseOrder> updateActivity(PurchaseOrder purchaseOrder){
//        // 构造查询条件包装
//        UpdateWrapper<PurchaseOrder> updateWrapper = new UpdateWrapper<PurchaseOrder>().eq("id", purchaseOrder.getId());
//        // 如果更新成功
//        if(SalesService.update(purchaseOrder, updateWrapper)){
//            return ResultUtil.success();
//        }
//        return ResultUtil.error(ResultCode.PROMOTION_UPDATE_ERROR);
//    }

    /**
     * 删除活动(逻辑删除)
     * @param id 活动id
     * @return 结果消息
     */
    @DeleteMapping("/{id}")
    public ResultMessage  deleteActivity(@PathVariable("id") String id){
        if(id == null){
            return ResultUtil.error(ResultCode.PROMOTION_DELETE_ERROR);
        }
        SalesService.removeById(id);
        return ResultUtil.success();
    }


}
